<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>品类管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            v-model="selectMsg.msg"
            clearable
            @clear="selectTypeList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="Select()"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table border stripe :data="typelist" row-key="id">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="id"
          label="类型Id"
          width="100px"
        ></el-table-column>
        <el-table-column prop="typeName" label="分类名称"></el-table-column>
        <el-table-column prop="isActived" label="是否禁用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isActived"
              @change="TypeStatechanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEidtDialog(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeTypeInfo(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.pager.currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.pager.total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加类型对话框区域 -->
    <el-dialog
      title="添加类型"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogclosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <!-- 类型名称 -->
        <el-form-item label="类型名称" prop="TypeName">
          <el-input v-model="addForm.TypeName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 底部按钮区域 -->
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddType()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑类型对话框区域 -->
    <el-dialog title="编辑类型" :visible.sync="editDialogVisible" width="50%">
      <!-- 内容主体区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
        @close="editDialogclosed"
      >
        <!-- 类型名称 -->
        <el-form-item label="类型名称" prop="TypeName">
          <el-input v-model="editForm.TypeName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 底部按钮区域 -->
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editType()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
export default {
  mounted () {
    this.rowDrop()
    this.getTypeList()
  },
  data () {
    return {
      // 搜索框内容
      selectMsg: {
        msg: '',
        currentPage: '',
        pageSize: '',
        total: ''
      },
      // 要操作的行
      currentIndex: 0,
      // 类型列表
      typelist: [
      ],
      // 分页用到的数据
      pager: {
        currentPage: 1,
        pageSize: 5,
        total: 0
      },
      // 控制添加类型对话框的显示与隐藏，默认为false
      addDialogVisible: false,
      // 添加类型的数据绑定
      addForm: {
        TypeName: ''
      },
      // 添加类型数据验证
      addFormRules: {
        TypeName: [
          { required: true, message: '请输入类型名称', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ]
      },
      // 控制编辑类型对话框的显示与隐藏，默认为false
      editDialogVisible: false,
      // 编辑类型获取到的数据
      editForm: {
        TypeName: ''
      },
      // 编辑类型数据验证
      editFormRules: {
        TypeName: [
          { required: true, message: '请输入类型名称', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 删除用户按钮
    async removeTypeInfo (id) {
      await this.$http.delete(`CommodityType/${id}`)
      this.getTypeList()
    },
    // 查找按钮
    Select () {
      if (this.selectMsg.msg) {
        this.SelectTypeName(this.selectMsg)
      } else {
        this.getTypeList(this.pager)
      }
    },
    // 类型名查寻方法
    async SelectTypeName (params) {
      console.log(params.msg)
      params.currentPage = 1
      params.pageSize = this.pager.pageSize
      params.total = this.pager.total
      const { data: res } = await this.$http.get('CommodityType/select/', { params: params })
      console.log(res)
      this.typelist = res.data
    },
    // 获取类型列表
    async getTypeList () {
      const { data: res } = await this.$http.get('commodityType', { params: this.pager })
      console.log(res)
      this.typelist = res.data
      this.pager.total = res.count
    },
    // 添加类型
    async AddType () {
      const { data: res } = await this.$http.post('commodityType', this.addForm)
      console.log(res)
      if (res.code === 1000) {
        this.typelist.push(res.data)
        this.$message.success('添加类型成功！')
      } else {
        this.$message.error('添加类型失败！')
      }
      this.addDialogVisible = false
    },
    // 行拖拽
    rowDrop () {
      // 获取当前表格
      const tbody = document.querySelector('.el-table__body-wrapper tbody')
      const that = this
      // newIndex -- 新下标 oldIndex -- 旧下标
      Sortable.create(tbody, {
        onEnd ({ newIndex, oldIndex }) {
          // 移除原来的数据
          const currRow = that.typelist.splice(oldIndex, 1)[0]
          // 移除原来的数据并插入新的数据
          that.typelist.splice(newIndex, 0, currRow)
          console.log(that.typelist)
          var newArray = that.typelist.slice(0)
          that.typelist = []
          that.$nextTick(function () {
            this.typelist = newArray
          }
          )
        }
      })
    },
    // 监听 switch 开关状态变化的事件
    async TypeStatechanged (row) {
      console.log(row)
      const { data: res } = await this.$http.put(`CommodityType/statu/${row.id}`, row)
      if (res.code !== 1000) {
        row.isActived = !row.isActived
        return this.$message.error('更新用户状态失败！')
      }
      return this.$message.success('更新用户状态成功！')
    },
    // 监听添加类型对话框关闭事件
    addDialogclosed () {
      this.addForm = {}
      this.$refs.addFormRef.resetFields()
    },
    // 监听编辑类型对话框关闭事件
    editDialogclosed () {
      this.editForm = {}
      this.$refs.editFormRef.resetFields()
    },
    // 展示编辑对话框
    showEidtDialog (row) {
      console.log(row)
      this.editDialogVisible = true
      this.editForm.TypeName = row.typeName
      this.editForm.id = row.id
    },
    // 编辑用户方法
    async editType () {
      console.log(this.editForm)
      const { data: res } = await this.$http.put(`CommodityType/${this.editForm.id}`, this.editForm)
      console.log(res)
      this.getTypeList()
      this.editDialogVisible = false
    },
    handleSizeChange (newSize) {
      // pagesize改变触发
      this.pager.pageSize = newSize
      this.getTypeList(this.pager)
    },
    handleCurrentChange (newPage) {
      // 页码改变触发
      this.pager.currentPage = newPage
      this.getTypeList(this.pager)
    },
    // 清空搜索框重新获取数据
    selectTypeList () {
      this.getTypeList()
    }
  }
}
</script>

<style scoped>
</style>
